package com.demo.hc.biz.task.service;

import com.demo.hc.biz.task.bean.TaskDistributeEnum;
import com.demo.hc.dal.dao.custom.TaskDistributeMapperCustom;
import com.demo.hc.model.mbg.TaskDistribute;
import com.demo.hc.model.request.TaskDistributeRequest;
import com.demo.hc.model.response.TaskDistributeResponse;
import com.demo.hc.model.view.TaskEstateView;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TaskDistributeService {

    @Autowired
    private TaskDistributeMapperCustom taskDistributeMapperCustom;

    public TaskDistributeResponse listTaskDistribute(TaskDistributeRequest requestParam){
        TaskDistributeResponse responseResult = new TaskDistributeResponse();
        /**	1.分页请求插件 */
        PageHelper.startPage(requestParam.getPageNo(),requestParam.getPageSize());
        List<TaskDistribute> list = taskDistributeMapperCustom.listTaskDistribute(requestParam);
        /**	2.分页返回 */
        PageInfo pageInfo = new PageInfo(list);
        responseResult.setPageNo(pageInfo.getPageNum());
        responseResult.setPageSize(pageInfo.getPageSize());
        responseResult.setTotal(pageInfo.getTotal());
        responseResult.setPageTotal(pageInfo.getPages());
        responseResult.setTaskEstateList(convertToView(list));
        return responseResult;
    }

    private List<TaskEstateView> convertToView(List<TaskDistribute> list){
        List<TaskEstateView> viewList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(list)){
            for(TaskDistribute task : list){
                TaskEstateView taskEstateView = new TaskEstateView();
                taskEstateView.setId(task.getId());
                taskEstateView.setCityId(task.getCityId());
                taskEstateView.setCityName(task.getCityName());
                taskEstateView.setDistrictId(task.getDistrictId());
                taskEstateView.setDistrictName(task.getDistrictName());
                taskEstateView.setAreaId(task.getAreaId());
                taskEstateView.setAreaName(task.getAreaName());
                taskEstateView.setEstateId(task.getEstateId());
                taskEstateView.setEstateName(task.getEstateName());
                taskEstateView.setSubEstateId(task.getSubEstateId());
                taskEstateView.setSubEstateName(task.getSubEstateName());
                taskEstateView.setStatus(task.getStatus());
                taskEstateView.setStatusName(TaskDistributeEnum.getName(task.getStatus()));
                viewList.add(taskEstateView);
            }
        }
        return viewList;
    }

}
